// DlgLogin.cpp : implementation file
//

#include "stdafx.h"
#include "finance.h"
#include "financedef.h"
//#include "CDataFile.h"
#include "CryptString.h"
#include "DlgLogin.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgLogin dialog


CDlgLogin::CDlgLogin(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLogin::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgLogin)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_bPermit = FALSE;
}


void CDlgLogin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgLogin)
	DDX_Control(pDX, ID_CHECK, m_btnOK);
	DDX_Control(pDX, IDCANCEL, m_btnCancel);
	DDX_Control(pDX, IDC_ED_PWD, m_edPwd);
	DDX_Control(pDX, IDC_ED_USER, m_edUser);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgLogin, CDialog)
	//{{AFX_MSG_MAP(CDlgLogin)
	ON_BN_CLICKED(ID_CHECK, OnCheck)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgLogin message handlers

// 
// 
//---------------------------------------------------------------------------------
BOOL CDlgLogin::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// <> Init UI	
	m_btnCancel.SetIcon(IDI_CANCEL34,IDI_CANCEL32);
	m_btnOK.SetIcon(IDI_LEDON, IDI_LEDOFF);

	m_edUser.SetLimitText( USERNAME_MAXLEN );
	m_edPwd.SetLimitText( PASSWORD_MAXLEN );

#ifndef _USERNAME_ON_
	m_edUser.EnableWindow( FALSE );
#endif	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// 
// 
//------------------------------------------------------------
void CDlgLogin::OnCheck() 
{
	CString		csUser, csPwd;
	CHAR		chPwd[ 100 ] = { 0 };
	CHAR		chDecrypted[ 100 ] = { 0 };
	

	// <> receive the text from edit
	UpdateData( TRUE );

	m_edPwd.GetWindowText( csPwd );
	if( csPwd.IsEmpty() ) 
	{
		MessageBox( "Password must be input!", "Warning" , MB_ICONWARNING );
		m_edPwd.SetFocus();
		return ;
	}
	
	// <> get password form Register
	HKEY	hRegKey = NULL;
	if( RegOpenKeyEx( HKEY_CURRENT_USER, IDS_REGSRBKEY, 0, KEY_QUERY_VALUE, &hRegKey ) == ERROR_SUCCESS ) 
	{		
		DWORD	dwLength = 100*sizeof(CHAR);
		DWORD	dwType = REG_BINARY;

		if( RegQueryValueEx( hRegKey, _T("Password"), NULL, &dwType, (BYTE*)chPwd, &dwLength ) != ERROR_SUCCESS )
			GetAPDefaultPwd( chPwd );	// Use default pwd when error
		
		::RegCloseKey( hRegKey );	
	}
	else
		GetAPDefaultPwd( chPwd );	// Use default pwd when error
	

	// <> decrypt username and password
	// Init the crypto class
	CCryptString oCrypt;
	
	// decrypt password
	oCrypt.DecryptString( chPwd, chDecrypted, IDS_PWDKEY );
	// compare password
	if( csPwd != chDecrypted ) 
	{
		MessageBox( "Invalid Password!", "Error" , MB_ICONERROR );
		m_edPwd.SetFocus();
		return ;
	}

	
	// username and password checked ok!
	m_bPermit = TRUE;
	
	CDialog::OnOK();
}

// 
// 
//------------------------------------------------------------
BOOL CDlgLogin::IsCheckedOK()
{
	return	m_bPermit;
}
